package com.whl.org.controller;

import com.whl.basic.annotation.PreAuthorize;
import com.whl.basic.util.AjaxResult;
import com.whl.basic.util.PageList;
import com.whl.org.domain.Department;
import com.whl.org.query.DepartmentQuery;
import com.whl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理接口
 *
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门接口", description = "部门接口的CRUD操作")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    // http://localhost:8080/department/1 + {id} + get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条记录")
    @PreAuthorize(name = "查询一条记录",sn = "department:getOne")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 查询所有记录
     * @return Department
     */
    @GetMapping
    // http://localhost:8080/department + 无 + get
    @ApiOperation(value = "查询所有记录")
    @PreAuthorize(name = "查询所有记录",sn = "department:getAll")
    public List<Department> queryAll(){
        return departmentService.findAll();
    }

    /**
     * 删除一条记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    // http://localhost:8080/department/1 + {id} + delete
    @ApiOperation(value = "删除一条记录")
    @PreAuthorize(name = "删除一条记录",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改数据
     * @param department
     * @return
     */
    @PutMapping
    // http://localhost:8080/department/ + {json对象} + put
    @ApiOperation(value = "添加或修改数据")
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页和高级查询
     * @return
     */
    @PostMapping
    // http://localhost:8080/department/ + {json对象} + post
    @ApiOperation(value = "分页和高级查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    // http://localhost:8080/department {id} + Patch
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树")
    @PreAuthorize(name = "查询部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
